package com.zx.sdk.listener;

import static com.zx.sdk.ZxSDK.REWARD;

import android.app.Activity;
import android.app.AlertDialog;

import androidx.annotation.NonNull;

import com.zx.sdk.StrategyHelper;
import com.zx.sdk.ZxSDK;
import com.zx.sdk.league.member.BaseLeagueMember;
import com.zx.sdk.model.AdInfo;
import com.zx.sdk.model.ZxError;
import com.zx.sdk.util.ConfigHelper;
import com.zx.sdk.util.LogHelper;
import com.zx.sdk.util.ToastHelper;
import com.zx.sdk.util.ZxEventHelper;

import java.util.Map;

public abstract class BaseZxRewardListener implements ZxRewardAdListener {

    private OnPreloadErrorListener mOnPreloadErrorListener;
    private OnShowErrorListener mOnShowErrorListener;
    private OnADExposeListener onADExposeListener;

    @Override
    public void onADClick(BaseLeagueMember<?, ?, ?, ?,?> member, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告", "onADClick", " appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        ZxEventHelper.record(REWARD, ZxEventHelper.CLICK, adInfo, member);
        Activity topActivity = ZxSDK.topActivityRef.get();
        if (topActivity != null && !topActivity.isDestroyed() && ConfigHelper.needWarningTips(member)) {
            new AlertDialog.Builder(topActivity)
                    .setTitle("温馨提示")
                    .setMessage(WARNING_TIP)
                    .setPositiveButton("确定", null)
                    .show();
        } else {
            ToastHelper.showToast(WARNING_TIP);
        }
        onADClick();
    }

    @Override
    public void onADClose(BaseLeagueMember<?, ?, ?, ?,?> member, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告" + "onADClose", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        onADClose();
    }

    @Override
    public void onADExpose(BaseLeagueMember<?, ?, ?, ?,?> member, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告", "onADExpose", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        ZxEventHelper.record(REWARD, ZxEventHelper.EXPOSE, adInfo, member);
        ZxSDK.notifyClearBidding(REWARD, adInfo.getMapPid());
        onADExpose();
        if (adInfo.getAdType() == AdInfo.AD_TYPE_EXPRESS_INTERSTITIAL_AD) {
            ConfigHelper.showInterstitial();
        } else if (adInfo.getAdType() == AdInfo.AD_TYPE_BANNER_AD) {
            ConfigHelper.showBanner();
        }
        if (this.onADExposeListener != null) {
            this.onADExposeListener.onADExpose();
        }
    }

    @Override
    public void onADLoad(BaseLeagueMember<?, ?, ?, ?,?> member, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告", "onADLoad", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        ZxEventHelper.record(REWARD, ZxEventHelper.LOADED, adInfo, member);
        onADLoad();
    }

    @Override
    public void onReward(BaseLeagueMember<?, ?, ?, ?,?> member, @NonNull Map<String, Object> map, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告", "onReward", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());

        int price;
        try {
            price = Integer.parseInt(StrategyHelper.getLastWinPrice(REWARD));
        } catch (Exception e) {
            price = adInfo.getCpm();
        }
        ZxSDK.uploadHitInformation(adInfo.getMapAppid(), adInfo.getMapPid(), price, 0);

        ZxEventHelper.record(REWARD, ZxEventHelper.REWARD, adInfo, member);
        onReward(map);
    }

    @Override
    public void onVideoCached(BaseLeagueMember<?, ?, ?, ?,?> member, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告", "onVideoCached", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        onVideoCached();
    }

    @Override
    public void onVideoComplete(BaseLeagueMember<?, ?, ?, ?,?> member, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告", "onVideoComplete", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        onVideoComplete();
    }

    @Override
    public void onPreLoadADError(BaseLeagueMember<?, ?, ?, ?,?> member, ZxError error, AdInfo adInfo) {
        LogHelper.e("联盟=" + member.getName(), "Reward广告", "onPreLoadADError  error = " + error + "", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        onPreLoadADError(error);
        if (mOnPreloadErrorListener != null) {
            mOnPreloadErrorListener.onError(error, adInfo);
        }
    }

    @Override
    public void onRewardStepVerify(BaseLeagueMember<?, ?, ?, ?,?> member, int taskType, int currentTaskStatus, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告", "onRewardStepVerify", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        onRewardStepVerify(taskType, currentTaskStatus);
    }

    @Override
    public void onAdSkip(BaseLeagueMember<?, ?, ?, ?,?> member, float v, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告", "onAdSkip", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        onAdSkip(v);
    }

    @Override
    public void onVideoPlayError(BaseLeagueMember<?, ?, ?, ?,?> member, ZxError error, AdInfo adInfo) {
        LogHelper.d("联盟=" + member.getName(), "Reward广告", "onVideoPlayError  error = " + error + "", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        onVideoPlayError(error);
    }

    @Override
    public void onNoAD(BaseLeagueMember<?, ?, ?, ?,?> member, ZxError error, AdInfo adInfo) {
        LogHelper.e("联盟=" + member.getName(), "Reward广告", "onNoAD error = " + error + "", "appId=" + adInfo.getMapAppid(), "pid=" + adInfo.getMapPid());
        onNoAD(error);
        if (mOnShowErrorListener != null) {
            mOnShowErrorListener.onError(error, adInfo);
        }
    }

    @Override
    public void setOnPreloadErrorListener(OnPreloadErrorListener listener) {
        mOnPreloadErrorListener = listener;
    }

    @Override
    public void setOnShowErrorListener(OnShowErrorListener listener) {
        mOnShowErrorListener = listener;
    }

    @Override
    public void setOnADExposeListener(OnADExposeListener onADExposeListener) {
        this.onADExposeListener = onADExposeListener;
    }

    public abstract void onADClick();

    public abstract void onADClose();

    public abstract void onADExpose();

    public abstract void onADLoad();

    public abstract void onReward(@NonNull Map<String, Object> map);

    public abstract void onVideoCached();

    public abstract void onVideoComplete();

    public abstract void onPreLoadADError(ZxError error);

    public abstract void onRewardStepVerify(int taskType, int currentTaskStatus);

    public abstract void onAdSkip(float v);

    public abstract void onVideoPlayError(ZxError error);

    public abstract void onNoAD(ZxError error);
}
